package com.swss.g.user.action;

import java.util.ArrayList;
import java.util.List;

import com.swss.g.base.BaseAction;
import com.swss.g.user.bean.Module;
import com.swss.g.user.bean.Popedom;
import com.swss.g.user.bean.Role;
import com.swss.g.user.bean.RolePopedom;
import com.swss.g.user.service.PopedomService;
import com.swss.g.user.service.RoleService;
import com.swss.util.Pagination;

public class RoleAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3016348148306946228L;
	/**
	 * bean
	 */
	private String url;

	private Role role;

	private List<Module> modList;

	private int popedomIds[];

	private List<Popedom> popedomList;

	private List<RolePopedom> rolePopedomList;

	@SuppressWarnings("unchecked")
	private Pagination pagination;

	private RolePopedom rolePopedom;

	/**
	 * service
	 * 
	 * @return
	 */

	private PopedomService popedomService;
	private RoleService roleService;

	public String add() {
		try {

			this.getRoleService().save(role);

			// userList = this.getUserService().findAll();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	};

	public String del() {
		try {

			this.getRoleService().delete(role);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String update() {
		try {

			this.getRoleService().update(role);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String init() {
		// this.msg="保存成功";
		// 临时
		if (pagination == null) {
			pagination = new Pagination();

		}
		pagination.setUsepage(false);
		pagination.setOrderBy("queue");
		pagination.setOrderType("asc");
		
		try {
			this.getPopedomService().findAll(pagination);
			popedomList = pagination.getResult();
			
			if (!rolePopedom.getRoleId().equals("")) {

				Pagination p = new Pagination();
				p.setUsepage(false);

				this.rolePopedomList = this.getRoleService()
						.findRolePopedomByExample(rolePopedom, p);
				rolePopedom.setRoleId(rolePopedom.getRoleId());

			}

			//if (code != null) {
			//	this.jsonCode = this.getCodeMsgJson();
			//}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "roleInit";
	}

	public String find() {
		try {
			role = this.getRoleService().get(role.getId());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "roleUpdate";
	}

	public String savePopes() {

		try {

			for (RolePopedom rp : rolePopedomList) {
				rp.setRoleId(rolePopedom.getRoleId());
			}

			this.roleService.updateRolePopedom(rolePopedom.getRoleId(),
					rolePopedomList);

			this.code = 0;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String findAllRole() {

		try {
			// 临时
			if (pagination == null) {
				pagination = new Pagination();

			}
			pagination = this.getRoleService().findAll(pagination);
			pagination.setUrl("/swss" + this.getRequest().getServletPath());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return INPUT;
		}

		return SUCCESS;
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List<Module> getModList() {
		return modList;
	}

	public void setModList(List<Module> modList) {
		this.modList = modList;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int[] getPopedomIds() {
		return popedomIds;
	}

	public void setPopedomIds(int[] popedomIds) {
		this.popedomIds = popedomIds;
	}

	public List<RolePopedom> getRolePopedomList() {
		return rolePopedomList;
	}

	public void setRolePopedomList(List<RolePopedom> rolePopedomList) {
		this.rolePopedomList = rolePopedomList;
	}

	public RolePopedom getRolePopedom() {
		return rolePopedom;
	}

	public void setRolePopedom(RolePopedom rolePopedom) {
		this.rolePopedom = rolePopedom;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public List<Popedom> getPopedomList() {
		return popedomList;
	}

	public void setPopedomList(List<Popedom> popedomList) {
		this.popedomList = popedomList;
	}

	public PopedomService getPopedomService() {
		return popedomService;
	}

	public void setPopedomService(PopedomService popedomService) {
		this.popedomService = popedomService;
	}

}
